package com.ad.admin.interview;

import android.database.Cursor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 39466 on 2016/4/17.
 */
public class QuestionDao {

    public static void insert(Map<String,Object> map){
        String sql = "insert into ques_ans (question, answer) values (?, ?)";
        Globals.util.getWritableDatabase().execSQL(sql,new Object[]{map.get("question").toString(),map.get("answer").toString()});
    }
    public static List<Map<String,Object>> loadData(){
        List<Map<String,Object>> values = new ArrayList<>();
        String sql = "select id , question , answer from ques_ans";
        Cursor c = Globals.util.getWritableDatabase().rawQuery(sql,null);
        c.moveToFirst();
        while(!c.isAfterLast()){
            Map<String,Object> map = new HashMap<>();
            map.put("qustion",c.getInt(0)+" "+c.getString(1));
            map.put("answer",c.getString(2));
            values.add(map);
            c.moveToNext();
        }

        c.close();
        return values;

    }
    public static List<Map<String,Object>> loadDataPage(int pagenum, int pagesize, String keyword){
        List<Map<String,Object>> values = new ArrayList<>();
        String sql  = "select id , question , answer from ques_ans where question like ? limit ?,?";
        Cursor c = Globals.util.getReadableDatabase().rawQuery(sql, new String[]{"%" + keyword + "%", pagesize * pagenum - pagesize + "", pagesize + ""});
        c.moveToFirst();
        while(!c.isAfterLast()){
            Map<String, Object> map = new HashMap<>();
            map.put("question", c.getInt(0) + " " + c.getString(1));
            map.put("answer", c.getString(2));

            values.add(map);
            c.moveToNext();
        }
        c.close();
        return values;
    }
    public static int getCount(String keyword){
        int recordCount = 0;
        String sql = "select count(*) from ques_ans where question like ?";
        Cursor c = Globals.util.getReadableDatabase().rawQuery(sql, new String[]{"%" + keyword + "%"});
        c.moveToFirst();
        recordCount = c.getInt(0);
        c.close();
        return recordCount;
    }
    public static void deleteAllData() {
        String sql = "delete from ques_ans";
        Globals.util.getWritableDatabase().execSQL(sql);
    }
}
